#include "pch.h"
#include "seGameObjectManager.h"
#include "seGame.h"


seGameObjectManager::seGameObjectManager()
{
}

seGameObjectManager::~seGameObjectManager()
{
	std::for_each(_gameObjects.begin(),_gameObjects.end(),GameObjectDeallocator());
}

void seGameObjectManager::Add(int name, seVisibleGameObject* gameObject)
{
	_gameObjects.insert(std::pair<int,seVisibleGameObject*>(name,gameObject));
}

void seGameObjectManager::Remove(int name)
{
	std::map<int, seVisibleGameObject*>::iterator results = _gameObjects.find(name);
	if(results != _gameObjects.end() )
	{
		delete results->second;
		_gameObjects.erase(results);
	}
}

seVisibleGameObject* seGameObjectManager::Get(int name) const
{
	std::map<int, seVisibleGameObject*>::const_iterator results = _gameObjects.find(name);
	if(results == _gameObjects.end() )
		return NULL;
	return results->second;
	
}

int seGameObjectManager::GetObjectCount() const
{
	return _gameObjects.size();
}


void seGameObjectManager::DrawAll(sf::RenderWindow& renderWindow)
{
	std::map<int,seVisibleGameObject*>::const_iterator itr = _gameObjects.begin();
	while(itr != _gameObjects.end())
	{
		itr->second->Draw(renderWindow);
		itr++;
	}
}

void seGameObjectManager::UpdateAll()
{
  std::map<int,seVisibleGameObject*>::const_iterator itr = 
                                              _gameObjects.begin();

  while(itr != _gameObjects.end())
  {
    itr->second->Update();

	if(itr->second->GetPosition().y + itr->second->GetHeight() /2 > viewHeight)
	{
		//Remove(itr->first);
		//cout << "Usunieto " << itr->first << endl;
	}
    itr++;
  }
  
}